/**
 * Created by JasonChou on 16/5/3.
 */
import Promise from '../libs/promise'
import http from '../libs/http'

var apiList = {
    getCaptcha: api + 'user/registerCaptcha',
    login: api + 'user/register',
    loginByCode: api + 'wx/loginInfoByCode',
    loginByOpenId: api + 'wx/loginInfoByOpenId',
    refresh: api + 'login'
};

export default{
    isLogin: false,
    check: false,
    promise: new Promise(),
    autoLogin(){
        var out = this;
        out.codeOrOpenIdLogin();
    },

    login(phone, captcha, cb){
        var out = this;

        http.post({
            url: api + 'user/register', jsonp: true,
            data: {
                phone: phone,
                captcha: captcha,
                openId: db.wx_openId
            }
        }).then(function (obj) {
            out.isLogin = true;

            cb(obj);
            if (obj.code == 0) {

                db.set('userInfo', obj.data);
                out.promise.resolve();
            }
        });
    },

    codeOrOpenIdLogin() {
        var out = this;

        //初始化
        if (agent.isWechat) {

            if (db.wx_openId) {
                http.get({
                    url: api + 'wx/loginInfoByOpenId',
                    data: {
                        openId: db.wx_openId
                    },
                    jsonp: true
                }).then(function (obj) {
                    if (obj.code == 0) {
                        var data = obj.data;
                        if (data.user) {
                            out.isLogin = true;
                            db.set('userInfo', data.user);
                            out.promise.resolve();
                        }

                        out.done();
                    }
                })
            } else if (db.wx_code) {
                http.get({
                    url: api + 'wx/loginInfoByCode',
                    data: {
                        code: db.wx_code
                    },
                    jsonp: true
                }).then(function (obj) {
                        if (obj.code == 0) {
                            var data = obj.data;
                            if (data.user) {
                                out.isLogin = true;
                                db.set('userInfo', data.user);
                                out.promise.resolve();
                            }

                            document.cookie = 'wx_openId=' + data.openId;
                            db.wx_openId = data.openId;
                            out.done();
                        }
                    }
                )
            }

        } else if (agent.isClient) {
            //todo 凭借手机号调取登录接口
        } else {
            //测试用
            http.get({url: apiList.refresh, jsonp: true}).then(
                function (obj) {
                    if (obj.code == 0) {
                        var data = obj.data;

                        if (data.user) {
                            out.isLogin = true;

                            db.set('userInfo', data.user);
                            db.set('cart_count', data.cart_count);
                            //document.cookie = 'wx_openId=' + data.user.openId;
                            db.wx_openId = data.user.openId;

                            out.promise.resolve();
                        }
                    }
                });
        }
    },

    //登录状态验证完毕
    done() {
        this.check = true;
    }
}